package org.megha.blog.example.selenium;

import static org.junit.Assert.assertTrue;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.megha.blog.example.selenium.yahoo.HomePage;
import org.megha.blog.example.selenium.yahoo.SearchResultsPage;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

/**
 * A selenium/page-objects based test that searches for google on yahoo.com.
 * 
 * @author megha birmiwal
 */
public class YahooSearchTestUsingPageObjects {

	static Selenium selenium;
	private HomePage homePage;
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.yahoo.com");
		selenium.start();
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
		selenium.stop();
	}

	@Before
	public void setUp() throws Exception {
		homePage = new HomePage(selenium);
		homePage.open();
	}

	@After
	public void tearDown() throws Exception {
		selenium.close();
	}

	@Test
	public void testYahooSearchForGoogle() {
		assertTrue("could not open homepage", 
				homePage.isOnThisPage());

		SearchResultsPage resultsPage = homePage.searchFor("google");
		
		assertTrue("could not open result page", 
				resultsPage.isOnThisPage());
		assertTrue("google.com not found in results", 
				resultsPage.getSearchResultPageText().contains("google.com"));
	}
}
